package clientgui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.JTextPane;
import javax.swing.BoxLayout;
import java.awt.FlowLayout;
import java.awt.Component;
import javax.swing.Box;
import java.awt.GridLayout;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;

import message.Order;

import client.Client;
import client.Connector;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

@SuppressWarnings("unused")
public class MainWindow extends JFrame {
	private static final long serialVersionUID = 1L;
	private JTextField textField_clientId;
	private JTextField textField_symbol;
	private JTextField textField_qty;
	private JTextField textField_price;
	JTextPane textPane;
	JLabel lblHelp;

	/**
	 * Launch the application.
	 */
	// public static void main(String[] args) {
	// EventQueue.invokeLater(new Runnable() {
	// public void run() {
	// try {
	// MainWindow frame = new MainWindow();
	// frame.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// });
	// }

	public static void startClientGUI() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow frame = new MainWindow();
					Client.mainwindow = frame;
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public MainWindow() {
		setTitle("Client");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 580, 506);
		getContentPane().setLayout(null);

		textField_clientId = new JTextField();
		final JButton btnConnect = new JButton("connect");
		textField_symbol = new JTextField();
		final JButton btnSubmit = new JButton("Submit");
		final JLabel lblSymbol = new JLabel("Symbol");
		textField_qty = new JTextField();
		final JLabel lblQuantity = new JLabel("Quantity");
		textField_price = new JTextField();
		final JLabel lblPrice = new JLabel("Price");
		textPane = new JTextPane();// ////////////////////
		final JLabel lblClientid = new JLabel("ClientID:");
		lblHelp = new JLabel("please connect");
		lblHelp.setForeground(Color.BLUE);

		textField_clientId.setBounds(83, 11, 86, 20);
		getContentPane().add(textField_clientId);
		textField_clientId.setColumns(10);

		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String clientName = textField_clientId.getText();
				if ("".equals(clientName)) {
					lblHelp.setForeground(Color.RED);
					lblHelp.setText("Set ClientId before connect. Ex:client1");
					return;
				}else{
				lblHelp.setText("Do connection ... ");
				Client.doConnect(clientName);
				}
			}
		});
		btnConnect.setBounds(179, 10, 107, 23);
		getContentPane().add(btnConnect);

		final JComboBox<String> comboBoxBuySell = new JComboBox<>();
		comboBoxBuySell.setBounds(125, 70, 98, 20);
		getContentPane().add(comboBoxBuySell);
		comboBoxBuySell.addItem("BUY");
		comboBoxBuySell.addItem("SELL");

		textField_symbol.setBounds(27, 70, 86, 20);
		getContentPane().add(textField_symbol);
		textField_symbol.setColumns(10);

		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// SUBMIT ACTION
				Order o;
				try {
					o = ValidatorGUI.doNewOrder(textField_clientId.getText(),
							null, textField_symbol.getText(),
							(String) comboBoxBuySell.getSelectedItem(),
							textField_price.getText(), textField_qty.getText());
				} catch (Exception e1) {
					lblHelp.setText(e1.getMessage());
					return;
				}
				Client.registerOrder(o);
				Client.outQueue.add(o);
			}
		});
		btnSubmit.setBounds(441, 69, 75, 23);
		getContentPane().add(btnSubmit);

		lblSymbol.setBounds(27, 55, 46, 14);
		getContentPane().add(lblSymbol);

		textField_qty.setBounds(245, 70, 86, 20);
		getContentPane().add(textField_qty);
		textField_qty.setColumns(10);

		lblQuantity.setBounds(245, 55, 67, 14);
		getContentPane().add(lblQuantity);

		textField_price.setBounds(343, 70, 86, 20);
		getContentPane().add(textField_price);
		textField_price.setColumns(10);

		lblPrice.setBounds(343, 55, 46, 14);
		getContentPane().add(lblPrice);

		textPane.setBounds(27, 150, 515, 284);
		getContentPane().add(textPane);

		lblClientid.setBounds(27, 14, 46, 14);
		getContentPane().add(lblClientid);

		lblHelp.setBounds(30, 125, 512, 14);
		getContentPane().add(lblHelp);

		JLabel lblBuySell = new JLabel("Buy/Sell");
		lblBuySell.setBounds(125, 54, 56, 16);
		getContentPane().add(lblBuySell);

	}

//	void refresh() {
//		Client.mainwindow.textPane.setText("");
//	}
}
